package cn.good.payment.servcie;

import cn.good.dto.Page;
import cn.good.pojo.Order;
import cn.good.pojo.ServicePrice;
import cn.good.service.OrderService;
import cn.good.utils.Constant;
import cn.good.utils.EmptyUtils;
import cn.good.utils.MD5;
import cn.good.vo.OrderCreateVO;
import cn.good.vo.OrderSearchVO;
import cn.good.vo.PageListVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GDOrderServiceImpl implements GDOrderService{

    @Reference
    private OrderService orderService;

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Override
    public Order searchOrderById(Long id) {
        return orderService.searchOrderById(id);
    }

    /**
     * 查询订单列表
     * @param orderSearchVO
     * @return
     * @throws Exception
     */
    @Override
    public PageListVO<Order> searchOrderList(OrderSearchVO orderSearchVO) throws Exception {
        Map<String,Object> map = new HashMap<>();
        if(!EmptyUtils.isEmpty(orderSearchVO.getUserId()))
        map.put("userId",orderSearchVO.getUserId());

        if(!EmptyUtils.isEmpty(orderSearchVO.getDoctorId()))
            map.put("doctorId",orderSearchVO.getDoctorId());

        if(!EmptyUtils.isEmpty(orderSearchVO.getOrderStatus()))
            map.put("orderStatus",orderSearchVO.getOrderStatus());

        if(EmptyUtils.isEmpty(orderSearchVO.getPageNo())||orderSearchVO.getPageNo()<1)
           orderSearchVO.setPageNo(1);

        int count = orderService.searchListCount(map);

        Page page = new Page(orderSearchVO.getPageNo(), Constant.PAGE_NO,count);

        map.put("beginPos",page.getBeginPos());
        map.put("pageSize",Constant.PAGE_NO);

       List<Order> list = orderService.searchListByUserId(map);

        PageListVO<Order> pageListVO = new PageListVO();
        pageListVO.setData(list);
        pageListVO.setPageCount(page.getPageCount());
        pageListVO.setTotal(count);

        return pageListVO;
    }

    /**
     * 创建订单
     * @param orderCreateVO
     */
    @Override
    public Order createOrder(OrderCreateVO orderCreateVO) {
        Order order = new Order();
        ServicePrice servicePrice = orderService.searchInfoById(orderCreateVO.getServicePriceId());
        order.setUserId(orderCreateVO.getUserId());
        order.setDoctorId(Long.parseLong(servicePrice.getDutyName().toString()));
        order.setCreatedBy(orderCreateVO.getUserId());
        order.setPayAmount(servicePrice.getMoney());
        order.setCreationDate(new Date());
        order.setOrderStatus(0);

        StringBuilder sb = new StringBuilder(orderCreateVO.getUserId()+"");
        SimpleDateFormat sdf = new SimpleDateFormat(Constant.TOKEN_DATE_FORMAT);
        sb.append(sdf.format(new Date()));

        order.setOrderNo(MD5.getMd5(sb.toString(),32));

        orderService.saveOrder(order);

        return order;
    }

    /**
     * 根据编号查询订单
     * @param orderNo
     * @return
     */
    @Override
    public Order searchOrderByNo(String orderNo) {
        return orderService.searchOrderByOrderNo(orderNo);
    }

    @Override
    public void modifiOrder(String orderNo, String tradeNo) throws Exception {

        Map map = new HashMap();
        map.put("orderNo",orderNo);
        map.put("tradeNo",tradeNo);
        map.put("orderStatus",2);

        int result = orderService.updateOrderByOrderNo(map);
        if(result<1){
            throw new Exception("修改失败！");
        }
    }
}
